﻿using SkiaSharp;

namespace ApiLib.Utils;

/// <summary>
/// 图片工具
/// </summary>
public static class ImageUtil
{
    /// <summary>
    /// 
    /// </summary>
    public static class SkiaUtil
    {
        /// <summary>
        /// 压缩图片
        /// </summary>
        /// <param name="source">原文件位置</param>
        /// <param name="target">生成目标文件位置</param>
        /// <param name="maxWidth">最大宽度，根据此宽度计算是否需要缩放，计算新高度，默认-1为80%的宽度</param>
        /// <param name="quality">图片质量，范围0-100，默认80</param>
        public static void Compress(string source, string target, decimal maxWidth = -1, int quality = 80)
        {
            using var file = File.OpenRead(source);
            using var fileStream = new SKManagedStream(file);
            using var bitmap = SKBitmap.Decode(fileStream);
            var width = (decimal)bitmap.Width;
            var height = (decimal)bitmap.Height;
            var newWidth = width;
            var newHeight = height;
            if (maxWidth == -1)
            {
                newWidth = (decimal)((double)width * 0.8);
            }
            else if (width > maxWidth)
            {
                newWidth = maxWidth;
                newHeight = height / width * maxWidth;
            }

            var skImageInfo = new SKImageInfo((int)newWidth, (int)newHeight);
            using var resized = bitmap.Resize(skImageInfo, SKFilterQuality.Medium);
            if (resized != null)
            {
                using var image = SKImage.FromBitmap(resized);
                using var writeStream = File.OpenWrite(target);
                image.Encode(SKEncodedImageFormat.Jpeg, quality).SaveTo(writeStream);
            }
        }

        /// <summary>
        /// 压缩图片
        /// </summary>
        /// <param name="source">原文件位置</param>
        /// <param name="target">目标文件</param>
        /// <param name="infos">水印信息</param>
        public static void AddWaterMark(string source, string target, List<WaterMarkInfo> infos)
        {
            using var file = File.OpenRead(source);
            using var fileStream = new SKManagedStream(file);
            using var bitmap = SKBitmap.Decode(fileStream);
            var width = (decimal)bitmap.Width;
            var height = (decimal)bitmap.Height;

            using var surface = SKSurface.Create(new SKImageInfo((int)width, (int)height));
            var canvas = surface.Canvas;
            canvas.DrawBitmap(bitmap, 0, 0);
            foreach (var info in infos)
            {
                canvas.DrawText(info.Text, new SKPoint(info.X, info.Y), new SKPaint(new SKFont() { Size = info.Size }));
            }

            using var image = surface.Snapshot();

            using var writeStream = File.OpenWrite(target);
            image.Encode(SKEncodedImageFormat.Jpeg, 100).SaveTo(writeStream);
        }
    }
}

/// <summary>
/// 水印信息
/// </summary>
public class WaterMarkInfo
{
    /// <summary>
    /// 水印文本
    /// </summary>
    public string Text { get; set; } = "";

    /// <summary>
    /// 字体
    /// </summary>
    public string Family { get; set; } = "";

    /// <summary>
    /// 字体大小
    /// </summary>
    public float Size { get; set; } = 12F;
    public int X { get; set; }
    public int Y { get; set; }
}